{$R-}

(*
	GetCreator -- map OSType to creator name
	By Dan Winkler.  DO NOT call the author!  Contact Apple Developer 
	Support on AppleLink "MacDTS" or on MCI "MacTech".

	©Apple Computer, Inc. 1987
	All Rights Reserved.

	pascal GetCreator.p
	link -m ENTRYPOINT -o {BOOT}documents -rt XFCN=1 -sn Main=GetCreator ¶
	  GetCreator.p.o {MPW}Libraries:Interface.o {MPW}PLibraries:PasLib.o 


*)

{$S GetCreator } 	{ Segment name must be the same as the command name. }

UNIT DummyUnit;

INTERFACE

USES MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;

PROCEDURE EntryPoint(paramPtr: XCmdPtr);
	
IMPLEMENTATION

TYPE Str31 = String[31];

PROCEDURE GetCreator(paramPtr: XCmdPtr); 						FORWARD;

  PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  BEGIN
    GetCreator(paramPtr);
  END;

  PROCEDURE GetCreator(paramPtr: XCmdPtr);
  VAR creator: OSType;
      appName: Str255;
      
  {$I XCmdGlue.inc }

    FUNCTION CreatorToString(creator: OsType): Str255;
    
    {  Given a document's 4-byte creator, find the application's name.	}
    {  Opens APPL resource 0 in DeskTop, and scans table for first match.	}
    {  Returns empty string if can't open desktop or creator not found.  	}
    
    LABEL 9;
    
    TYPE ApplRecord =  RECORD
			 creator: 	OsType;
			 directory: LongInt;
			 fileName:	Str255;
		       END;
    
    VAR refNum: 	INTEGER;
	errNum: 	INTEGER;
	hndl: 	HANDLE;
	applPtr: 	^ApplRecord;
	size,limit: LongInt;
	bump:  	INTEGER;
	wdParams:	WDPBRec;
	result:   INTEGER;
    
    BEGIN
      CreatorToString := '';
      IF creator = 'APPL' THEN EXIT(CreatorToString);
      SetResLoad(FALSE);   { avoid slow preloads }
      
      (** refNum := OpenResFile('DeskTop'); **)
      
      { set up working directory for current path }
      ZeroBytes(@wdParams,SizeOf(wdParams));
      WITH wdParams DO
	BEGIN
	  ioWDProcID := $4552494B;  		{ 'ERIK' so finder will delete later }
	  ioWDDirID := 2;				{ root }
	END;
      result := PBOpenWD(@wdParams,FALSE);
      refNum := OpenRFPerm('DeskTop',wdParams.ioVRefNum,fsRdPerm);
      
      SetResLoad(TRUE);
      IF refnum = -1 THEN EXIT(CreatorToString);
      hndl := GetResource('APPL',0);
      IF hndl = NIL THEN GOTO 9;
      size := SizeResource(hndl);
      applPtr := Pointer(hndl^);
      limit := ORD(applPtr) + size;
      REPEAT
	IF applPtr^.creator = creator THEN  { we found it }
	  BEGIN
	    CreatorToString := applPtr^.fileName;
	    GOTO 9;
	  END;
	bump := 9 + Length(applPtr^.fileName);
	IF ODD(bump) THEN bump := bump + 1;
	applPtr := Pointer(ORD(applPtr) + bump);
      UNTIL ORD(applPtr) >= limit;
    9: CloseResFile(refNum);
    END;      
  
  BEGIN
    WITH paramPtr^ DO
      BEGIN
        IF paramCount < 1 THEN EXIT(GetCreator);
	BlockMove(params[1]^,@creator,4);
	appName := CreatorToString(creator);
        IF appName <> '' THEN returnValue := PasToZero(appName);
      END;
  END;


END.



